<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="题目内容" prop="questionContent">
        <el-input
          v-model.tirm="queryParams.questionContent"
          placeholder="请输入题目内容"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="题目类型" prop="optionType">
        <el-select
          v-model="queryParams.optionType"
          placeholder="请选择题目类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in optionTypeList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="primary" plain @click="openForm('create')">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :row-key="(row) => row.id">
      <el-table-column
        label="题目内容"
        align="center"
        prop="questionContent"
        show-overflow-tooltip
      />
      <el-table-column label="题目类型" align="center" prop="optionType">
        <template #default="scope"> {{ getLabel(optionTypeList, scope.row.optionType) }} </template>
      </el-table-column>
      <el-table-column label="题目解析" align="center" prop="analysis" show-overflow-tooltip />
      <el-table-column label="操作" align="center" fixed="right" min-width="160">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <QuestionForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="Question">
import QuestionForm from './QuestionForm.vue'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(false) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  questionContent: null,
  optionType: null
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const saved = window.localStorage.getItem('questionList')
    const listData = saved ? JSON.parse(saved) : []
    list.value =
      listData.length > 0
        ? listData
        : [
            {
              id: '6635494501',
              questionStatus: 1,
              optionType: 1,
              questionContent: '测试单选题',
              analysis: '这题什么选项会怎么样',
              optionList: [
                {
                  optionSort: 0,
                  optionKey: 'A',
                  optionContent: '单选题答案1',
                  optionAnalysis: '选1答案会怎么样'
                },
                {
                  optionSort: 1,
                  optionContent: '单选题答案2',
                  optionKey: 'B',
                  optionAnalysis: '选2答案会怎么样'
                },
                {
                  optionSort: 2,
                  optionContent: '单选题答案3',
                  optionKey: 'C',
                  optionAnalysis: '选3答案会怎么样'
                },
                {
                  optionSort: 3,
                  optionContent: '单选题答案4',
                  optionKey: 'D',
                  optionAnalysis: '选4答案会怎么样'
                }
              ]
            },
            {
              id: '4364727825',
              questionStatus: 1,
              optionType: 2,
              questionContent: '测试多选题',
              analysis: '什么选项会怎么样',
              optionList: [
                {
                  optionSort: 0,
                  optionKey: 'A',
                  optionContent: '多选答案1',
                  optionAnalysis: '答案1怎么怎么样'
                },
                {
                  optionSort: 1,
                  optionContent: '多选答案2',
                  optionKey: 'B',
                  optionAnalysis: '答案2怎么怎么样'
                },
                {
                  optionSort: 2,
                  optionContent: '多选答案3',
                  optionKey: 'C',
                  optionAnalysis: '答案3怎么怎么样'
                },
                {
                  optionSort: 3,
                  optionContent: '多选答案4',
                  optionKey: 'D',
                  optionAnalysis: '答案4怎么怎么样'
                }
              ]
            },
            {
              id: '2064779989',
              questionStatus: 1,
              optionType: 3,
              questionContent: '测试判断题',
              analysis: '选对会怎么样,选错会怎么样',
              optionList: [
                {
                  optionSort: 0,
                  optionKey: 'A'
                }
              ]
            },
            {
              id: '8468475372',
              questionStatus: 1,
              optionType: 4,
              questionContent: '测试简答题',
              analysis: '这题的答案怎么样',
              optionList: [
                {
                  optionSort: 0,
                  optionKey: 'A'
                }
              ]
            }
          ]
    // 把题目存到store
    window.localStorage.setItem('questionList', JSON.stringify(list.value))
    if (queryParams.questionContent)
      list.value = list.value.filter((item) => item.questionContent == queryParams.questionContent)
    if (queryParams.optionType)
      list.value = list.value.filter((item) => item.optionType == queryParams.optionType)
    total.value = list.value.length
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    list.value = list.value.filter((item) => item.id != id)

    // 把题目存到store
    window.localStorage.setItem('questionList', JSON.stringify(list.value))
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

// 题目类型
const optionTypeList = reactive([
  { value: 1, label: '单选题' },
  { value: 2, label: '多选题' },
  { value: 3, label: '判断题' },
  { value: 4, label: '简答题' }
])

const getLabel = (list: Array<any>, value: any) => {
  const dictLabel = ref('')
  list.forEach((dict: any) => {
    if (dict.value == value) {
      dictLabel.value = dict.label
    }
  })
  return dictLabel.value
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style scoped>
.image-slot {
  display: flex;
  width: 100%;
  height: 100%;
  color: var(--el-text-color-secondary);
  background: var(--el-fill-color-light);
  justify-content: center;
  align-items: center;
}
</style>
